package com.rpc.protocol;

import java.io.*;

/**
 * @author yanzhuoleo
 * @version V1.0
 * @Description:
 * @date 2018-01-16-下午6:32
 */
public class JdkSerializeImpl implements ISerialize {


    @Override
    public byte[] serialize(Object transportData) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        ObjectOutputStream objectOut;
        try {
            objectOut = new ObjectOutputStream(output);
            objectOut.writeObject(transportData);
            objectOut.close();
            output.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return output.toByteArray();
    }

    /**
     * TODO objectClass未修复
     * @param data 序列化byte数据
     * @param objectClass
     * @return
     */
    @Override
    public Object deserialize(byte[] data, Class objectClass) {
        ByteArrayInputStream input = new ByteArrayInputStream(data);
        ObjectInputStream objectIn;
        Object object = null;
        try {
            objectIn = new ObjectInputStream(input);
            object = objectIn.readObject();
            objectIn.close();
            input.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return object;
    }

    public static void main(String[] args) {


    }

}
